layui.use(['laydate','element','layer'], function(){
    $ = layui.jquery;//jquery
  laydate = layui.laydate;//日期插件
  lement = layui.element();//面包导航
  layer = layui.layer;//弹出层

  var start = {
    min: laydate.now()
    ,max: '2099-06-16 23:59:59'
    ,istoday: false
    ,choose: function(datas){
      end.min = datas; //开始日选好后，重置结束日的最小日期
      end.start = datas //将结束日的初始值设定为开始日
    }
  };
  
  var end = {
    min: laydate.now()
    ,max: '2099-06-16 23:59:59'
    ,istoday: false
    ,choose: function(datas){
      start.max = datas; //结束日选好后，重置开始日的最大日期
    }
  };
  
  /*document.getElementById('LAY_demorange_s').onclick = function(){
    start.elem = this;
    laydate(start);
  }
  document.getElementById('LAY_demorange_e').onclick = function(){
    end.elem = this
    laydate(end);
  }*/
  
});
$(function(){
	initLayPage();
});
function search(){
	pageConfig ={};
	pageConfig.pageSize = 10;
    pageConfig.pageNo = 1;
	pageConfig.nickname = $("#nickname").val();
	pageConfig.phone = $("#phone").val();
	initLayPage(pageConfig);
}
function initLayPage(pageConf) {
     if(!pageConf){
         pageConf ={};
         pageConf.pageSize = 10;
         pageConf.pageNo = 1;
     }
     $.post("../../user/findList", pageConf, function (data) {
    	 var pages = data.reObj.totalPage;
    	 var total = data.reObj.totalCount;
    	 $("#total").html(total);
         layui.use(['laypage', 'layer'], function () {
             var laypage = layui.laypage;
             laypage({
                 cont: 'page'
                 ,pages: pages
                 ,limit:10
                 ,first: 1
                 ,last: pages
                 ,curr: data.reObj.pageNo
                 ,prev: '<em><</em>'
                 ,next: '<em>></em>'
                 ,jump: function(obj, first){
                	 if(!first){
                		 pageConf.pageNo = obj.curr;
                         pageConf.pageSize = obj.limit;
                         pageConf.nickname = $("#nickname").val();
                         pageConf.phone = $("#phone").val();
                		 initLayPage(pageConf);
                	 }
                 }
            }); 
            fillTable(data.reObj.content); //页面填充
         })
     });
}
 //填充表格数据
function fillTable(data) {
	 var userList = data;
     var info = '';
     for(var i = 0;i<data.length;i++){
    	 info += '<tr><td><input type="checkbox" value="" name=""></td><td>'+userList[i].id+'</td><td>'+userList[i].nickname+'</td><td><img src="'+userList[i].profilePhoto+'" style="width:40px;height:40px"></td><td>'+userList[i].phone+'</td><td>'+userList[i].openid+'</td><td>'+(userList[i].status==0?"正常":"停用")+'</td><td>'+userList[i].balance+'</td><td>'+getLocalTime(userList[i].updateTime)+'</td><td>'+getLocalTime(userList[i].createTime)+'</td><td class="td-manage"><button class="layui-btn layui-btn-small" onclick="admin_edit(\'用户详情\',\'userInfo.jsp?id='+userList[i].id+'\',\'500\',\'500\')" >查看</button><button class="layui-btn layui-btn-small" onclick="admin_del(this,'+userList[i].id+')">删除</button></td>';
     }
     $("#data").html(info);
}
function getLocalTime(nS) {     
	var unixTimestamp = new Date(nS) ;
	commonTime = unixTimestamp.toLocaleString();
	return commonTime;
}
//批量删除提交
 function delAll () {
    layer.confirm('确认要删除吗？',function(index){
        //捉到所有被选中的，发异步进行删除
        layer.msg('删除成功', {icon: 1});
    });
 }
 /*添加*/
function admin_add(title,url,w,h){
    x_admin_show(title,url,w,h);
}

 /*停用*/
function admin_stop(obj,id){
    layer.confirm('确认要停用吗？',function(index){
        //发异步把用户状态进行更改
        $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="admin_start(this,id)" href="javascript:;" title="启用"><i class="layui-icon">&#xe62f;</i></a>');
        $(obj).parents("tr").find(".td-status").html('<span class="layui-btn layui-btn-disabled layui-btn-mini">已停用</span>');
        $(obj).remove();
        layer.msg('已停用!',{icon: 5,time:1000});
    });
}

/*启用*/
function admin_start(obj,id){
    layer.confirm('确认要启用吗？',function(index){
        //发异步把用户状态进行更改
        $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" onClick="admin_stop(this,id)" href="javascript:;" title="停用"><i class="layui-icon">&#xe601;</i></a>');
        $(obj).parents("tr").find(".td-status").html('<span class="layui-btn layui-btn-normal layui-btn-mini">已启用</span>');
        $(obj).remove();
        layer.msg('已启用!',{icon: 6,time:1000});
    });
}
//编辑
function admin_edit (title,url,w,h) {
    x_admin_show(title,url,w,h); 
}
/*删除*/
function admin_del(obj,id){
	
	layer.confirm('确认要删除吗？',function(index){
        //发异步删除数据
		$.ajax({
			url:'../../user/del',
			type:'post',
			dataType:'json',
			data:{
				ids:id
			},
			success:function(data){
				if(data.code==1){
					layer.alert('已删除!',{icon:1},function () {
						window.location.reload();
					})
				}
			}
		});
	})
}
